package org.edu.modules.research.rfunds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.research.rfunds.entity.VeJkyFundList;
import org.edu.modules.research.rfunds.mapper.VeJkyFundMapper;
import org.edu.modules.research.rfunds.entity.VeJkyFund;
import org.edu.modules.research.rfunds.dto.VeJkyFundDto;
import org.edu.modules.research.rfunds.service.VeJkyFundListService;
import org.edu.modules.research.rfunds.service.VeJkyFundService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.edu.modules.research.rfunds.service.VeJkyFundTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * 实现类
 */
@Service
public class VeJkyFundServiceImpl extends BaseServiceImpl<VeJkyFund, VeJkyFundMapper> implements VeJkyFundService {


    @Autowired
    private VeJkyFundListService listService;

    public IPage<VeJkyFundDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeJkyFundDto findDtoById(String id) {
        VeJkyFundDto dto = mapper.findDtoById(id);

        LambdaQueryWrapper<VeJkyFundList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VeJkyFundList::getFundId,id);
        List<VeJkyFundList> veJkyFundLists = listService.listByWrapper(queryWrapper);
        dto.setDetails(veJkyFundLists);

        return dto;
    }

    /**
    * 新增或者修改
    */
    @Transactional
    public void insertOrUpdate(VeJkyFundDto dto,Boolean isAdd){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        dto.setCreateTime(new Date());
        dto.setUserId(sysUser.getId());
        dto.setUserName(sysUser.getRealname());

        if(isAdd){
            super.insert(dto);
        }else{
            listService.deleteByPid(dto.getId());
            super.update(dto);
        }

        for (VeJkyFundList detail: dto.getDetails()) {
            detail.setFundId(dto.getId());
        }
        listService.insert(dto.getDetails());



    }

}
